// pages/user/user-register-page.js

import { RequestManager } from '../../util/requestManager.js'
let requestManager = new RequestManager()

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPhoneX: false,
    name:'',
    gender: 1,
    birthday: '2019-08-02',
    phone:'',
    code: '',
    introducer:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isPhoneX: app.globalData.isIphoneX
    })
  },

  //  获取生日
  bindDateChange:function(event){
    this.setData({
      date: event.detail.value
    })
  },
  //  获取验证码
  action_getCode:function(event){
    console.log('获取验证码')
  },
  //  性别切换事件
  action_gender:function(event){
    var key = event.target.dataset.key
    this.setData({
      gender: key === "man" ? 1 : 2
    })
  },
  //  input文本编辑事件
  textDidEndEdit: function (event) {
    var text_id = event.target.id;
    var textValue = event.detail.value;
    if (text_id === "name") {
      this.setData({
        name: textValue
      })
    }
    else if (text_id === "phone") {
      this.setData({
        phone: textValue
      })
    }
    else if (text_id === "code") {
      this.setData({
        code: textValue
      })
    }
    else if (text_id === "introducer") {
      this.setData({
        introducer: textValue
      })
    }
  },
  //  提交资料信息
  action_submit:function(event){

    // var code = "011KewVv1S91wh0GN7Vv1RYuVv1KewVh"
    //  var openid = "o1CGg4nikpL7thWKqtUuF6id3OGc"
    //  var session_key = "xVY4yjEC2gSvACW17zEgaA"
    wx.login({
      success(res){
        console.log(res);
        var code = res.code;
        requestManager.getUserOpenId(code,function(info){
          console.log(info)
        })
      }
    })
    
    return;

    var errMsg = null;
    if(this.data.name.length == 0 && errMsg == null){
      errMsg = '姓名不能为空'
    }

    if (this.data.phone.length == 0 && errMsg == null) {
      errMsg = '手机号不能为空'
    }

    if (this.data.code.length == 0 && errMsg == null) {
      errMsg = '验证码不能为空'
    }

    if (errMsg != null) {
      wx.showToast({
        title: errMsg,
        icon: 'none'
      });
      return;
    }

    console.log('提交资料:name=', this.data.name, "phone:", this.data.phone, "birthday:", this.data.birthday, "code:", this.data.code, "introducer:", this.data.introducer, "gender:", this.data.gender)
  }
})